package com.probox.common.security.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.exception.ServiceException;
import com.probox.common.core.utils.SensitiveWordFilter;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.security.annotation.Sensitiveword;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;

import javax.annotation.Resource;


/**
 * @author panmou
 * @date 2022-05-18
 * 全局拦截校验器
 * BindingResult result
 */
@Aspect
@Component
public class ValidatorAspect {

    @Resource(name = "stringRedisTemplate")
    private ValueOperations<String, String> sops;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    private static final Logger log = LoggerFactory.getLogger(ValidatorAspect.class);

    // 医生端和pc端
    @Around("execution(* com.probox.common.entity.controller..*.*(..)) && args(..,bindingResult)")
    public Object doAround(ProceedingJoinPoint pjp, BindingResult bindingResult) throws Throwable {
        Signature signature = pjp.getSignature();
        if (bindingResult.hasErrors()) {
            log.error(signature.toString() + ":" + bindingResult.getFieldError().getDefaultMessage());
            throw new ServiceException("-1", bindingResult.getFieldError().getDefaultMessage());
        }
        return pjp.proceed();
    }


    // 患者端
    @Around("execution(* com.probox.api.controller..*.*(..)) && args(..,bindingResult)")
    public Object doAroundOpen(ProceedingJoinPoint pjp, BindingResult bindingResult) throws Throwable {
        Signature signature = pjp.getSignature();
        if (bindingResult.hasErrors()) {
            log.error(signature.toString() + ":" + bindingResult.getFieldError().getDefaultMessage());
            throw new ServiceException("-1", bindingResult.getFieldError().getDefaultMessage());
        }
        return pjp.proceed();
    }

//
//    @Around("execution(* com.probox.api.controller..*.*(..)) && @annotation(sensitiveword)")
//    public Object handleRequest(ProceedingJoinPoint pjp, Sensitiveword sensitiveword) throws Throwable {
//        // 入参
//        String type = sensitiveword.type();
//        String obj = sops.get(CachePrefix.SENSTIVEWORD);
//        if (ObjectUtil.isNotEmpty(obj)) {
//            obj = obj.replaceAll("\\\\", "");
//            obj = obj.substring(1, obj.length() - 1);
//            JSONObject json = JSONObject.parseObject(obj);
//            if (PublishTypeEnum.content.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("contentTitle"))) {
//                    String contentTitle = jsonObject.getString("contentTitle");
//                    sensitive = sensitiveWordFilter.filter(contentTitle);
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("contentDesc"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("contentDesc"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//
//            } else if (PublishTypeEnum.activity.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("activityTitle"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("activityTitle"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("activityDesc"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("activityDesc"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("locationName"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("locationName"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.location.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                String contentTitle = jsonObject.getString("locationTitle");
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(contentTitle)) {
//                    sensitive = sensitiveWordFilter.filter(contentTitle);
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("locationDesc"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("locationDesc"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("locationName"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("locationName"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.comment.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("commentContent"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("commentContent"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.sign.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("signName"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("signName"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("signRemark"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("signRemark"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//
//            } else if (PublishTypeEnum.booking.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("sensitiveWord"))) {
//                    sensitive = sensitiveWordFilter.filter(jsonObject.getString("sensitiveWord"));
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.orderTerms.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                StringBuffer buffer = new StringBuffer("");
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("termsContent"))) {
//                    buffer.append(jsonObject.getString("termsContent"));
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("tips"))) {
//                    buffer.append(jsonObject.getString("tips"));
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("cancelRemark"))) {
//                    buffer.append(jsonObject.getString("cancelRemark"));
//                }
//                String string = buffer.toString();
//                if (ObjectUtil.isNotEmpty(string)) {
//                    sensitive = sensitiveWordFilter.filter(string);
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.follow.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                sensitiveWordFilter.initializationWork(json.getString("contents"));
//                String sensitive = null;
//                StringBuffer buffer = new StringBuffer("");
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("followTitle"))) {
//                    buffer.append(jsonObject.getString("followTitle"));
//                }
//                if (ObjectUtil.isNotEmpty(jsonObject.getString("followDesc"))) {
//                    buffer.append(jsonObject.getString("followDesc"));
//                }
//                String string = buffer.toString();
//                if (ObjectUtil.isNotEmpty(string)) {
//                    sensitive = sensitiveWordFilter.filter(string);
//                    if (sensitive != null && sensitive.trim().length() > 0) {
//                        return AjaxResult.error(500, "包含敏感词,不能提交!");
//                    }
//                }
//            } else if (PublishTypeEnum.bict.getKey().equals(type)) {
//                Object arg = pjp.getArgs()[0];
//                String s = JSON.toJSONString(arg);
//                JSONArray objects = JSONObject.parseArray(s);
//
//                if (!CollectionUtils.isEmpty(objects)) {
//                    sensitiveWordFilter.initializationWork(json.getString("contents"));
//                    String sensitive = null;
//                    StringBuffer buffer = new StringBuffer("");
//                    objects.forEach(vo -> {
//                        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(vo));
//                        //arrival_reminder("arrival_reminder", "到达提醒"),
//                        if (ObjectUtil.isNotEmpty(jsonObject.getString("detailTypeCode"))) {
//                            if (ObjectUtil.isNotEmpty(jsonObject.getString("detailTypeValue"))) {
//                                if (LocationDetailType.parking_reminder.getKey().equals(jsonObject.getString("detailTypeCode"))) {
//                                    buffer.append(jsonObject.getString("detailTypeValue"));
//                                } else if (LocationDetailType.arrival_reminder.getKey().equals(jsonObject.getString("detailTypeCode"))) {
//                                    buffer.append(jsonObject.getString("detailTypeValue"));
//                                }
//                            }
//                        }
//                    });
//                    String string = buffer.toString();
//                    if (ObjectUtil.isNotEmpty(string)) {
//                        sensitive = sensitiveWordFilter.filter(string);
//                        if (sensitive != null && sensitive.trim().length() > 0) {
//                            return AjaxResult.error(500, "包含敏感词,不能提交!");
//                        }
//                    }
//                }
//            }
//
//        }
//        return pjp.proceed();
//    }
}
